package com.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.annotation.Resource;

@Configuration
@EnableAuthorizationServer // 认证服务器
public class MyOauthConfig extends AuthorizationServerConfigurerAdapter {

    /**
     * 第三方的客户端的信息
     * @param clients
     * @throws Exception
     */
    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private AuthenticationManager authenticationManager;
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
       //
        clients.inMemory()
                .withClient("admin") // client_id  登录名
                .secret(passwordEncoder.encode("123456")) // client_secret  密码
                .authorizedGrantTypes("password","refresh_token")// 授权的模式   授权码模式
                .accessTokenValiditySeconds(60*60)  // token 生效时间使一个小时
                .refreshTokenValiditySeconds(60*2*60)//
               // .redirectUris("http://localhost:8085")// 授权码 给你  从那里拿到  code
                .autoApprove(true)// 是否自动授权  true
                .scopes("all"); // 授权的范围;

    }

    /**
     * 设置凭证的信息
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
       endpoints.authenticationManager(authenticationManager);
       endpoints.tokenStore(getTokenStore());// tokenStore
        endpoints.accessTokenConverter(getConvert());// 生成jwt   解析jwt

    }

    /**
     * 存放jwt
     * @return
     */
    @Bean
    public JwtTokenStore getTokenStore() {
        JwtTokenStore jwtTokenStore = new JwtTokenStore(getConvert());
        return jwtTokenStore;
    }

    /**
     * 解析  生成jwt
     * @return
     */
    @Bean
    public JwtAccessTokenConverter getConvert() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("yyl");// 设置签名的key
        return jwtAccessTokenConverter;
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients()
                .checkTokenAccess("permitAll()")//
                .tokenKeyAccess("permitAll()");//
    }
}
